﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.Properties;
using InHD.dataaccess;
using InHD.commands;
using InHD.utilities;
using System.Windows;
using System.Data;

namespace InHD.viewmodels
{
    class CompanyInfoViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        #region Fields

        readonly CompanyInfo _companyInfo;
        readonly CompanyInfoRepository _companyInfoRepository;
        RelayCommand _saveCommand;
        #endregion // Fields

        #region Constructor

        public CompanyInfoViewModel(CompanyInfo companyInfo, CompanyInfoRepository companyInfoRepository)
        {
            if (companyInfo == null)
                throw new ArgumentNullException("customer");
            if (companyInfoRepository == null)
                throw new ArgumentNullException("companyInfoRepository");

            this._companyInfo = companyInfo;
            this._companyInfoRepository = companyInfoRepository;
        }
        #endregion // Constructor

        #region CompanyInfo Properties

        public string Email
        {
            get { return _companyInfo.Email; }
            set
            {
                if (value == _companyInfo.Email)
                    return;

                _companyInfo.Email = value;

                base.OnPropertyChanged("Email");
            }
        }

        public string Name
        {
            get { return _companyInfo.Name; }
            set
            {
                if (value == _companyInfo.Name)
                    return;

                _companyInfo.Name = value;

                base.OnPropertyChanged("Name");
            }
        }

        public string Address
        {
            get { return _companyInfo.Address; }
            set
            {
                if (value == _companyInfo.Address)
                    return;

                _companyInfo.Address = value;

                base.OnPropertyChanged("Address");
            }
        }

        public string Phone
        {
            get { return _companyInfo.Phone; }
            set
            {
                if (value == _companyInfo.Phone)
                    return;

                _companyInfo.Phone = value;

                base.OnPropertyChanged("Phone");
            }
        }

        public string Taxcode
        {
            get { return _companyInfo.Taxcode; }
            set
            {
                if (value == _companyInfo.Taxcode)
                    return;

                _companyInfo.Taxcode = value;

                base.OnPropertyChanged("Taxcode");
            }
        }

        public string Director
        {
            get { return _companyInfo.Director; }
            set
            {
                if (value == _companyInfo.Director)
                    return;

                _companyInfo.Director = value;

                base.OnPropertyChanged("Director");
            }
        }
        #endregion // Customer Properties

        #region Presentation Properties

        public override string DisplayName
        {
            get
            {
                return _companyInfo.Name;
            }
        }

        /// <summary>
        /// Returns a command that saves the customer.
        /// </summary>
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        (param) => this.Save(),
                        (param) => this.CanSave
                        );
                }
                return _saveCommand;
            }
        }

        #endregion // Presentation Properties

        #region Public Methods

        /// <summary>
        /// Saves the customer to the repository.  This method is invoked by the SaveCommand.
        /// </summary>
        public void Save()
        {
            if (!this._companyInfo.IsValid)
            {
                base.OnPropertyChanged("Director");
                base.OnPropertyChanged("Name");
                base.OnPropertyChanged("Email");
                base.OnPropertyChanged("Taxcode");
                return;
                //throw new InvalidOperationException(Resources.CompanyInfoViewModel_Exception_CannotSave);
            }

            if (_companyInfoRepository.GetCompanyInfos().Count > 0)
            {
                this._companyInfoRepository.EditCompany(this._companyInfo);
            }
            else
            {
                _companyInfoRepository.AddCompanyInfo(this._companyInfo);
            }
            
            base.OnPropertyChanged("DisplayName");
            OnRequestClose();
        }

        #endregion // Public Methods

        #region Private Helpers

        /// <summary>
        /// Returns true if the customer is valid and can be saved.
        /// </summary>
        bool CanSave
        {
            get { return _companyInfo.IsValid; }
        }

        #endregion // Private Helpers

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error
        {
            get { return (_companyInfo as IDataErrorInfo).Error; }
        }

        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error = null;

                error = (_companyInfo as IDataErrorInfo)[propertyName];


                // Dirty the commands registered with CommandManager,
                // such as our Save command, so that they are queried
                // to see if they can execute now.
                CommandManager.InvalidateRequerySuggested();

                return error;
            }
        }

        #endregion // IDataErrorInfo Members
    }
}
